package com.yeda.engine.controller;

import cn.hutool.core.util.ArrayUtil;
import com.yeda.engine.util.base.Result;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("test")
@Slf4j
public class RepositoryController {

    @Autowired
   private RepositoryService repositoryService;
    @RequestMapping("/save")
    public  String saveRepository(){
        Deployment deployment = repositoryService.createDeployment()
                // bpmn文 件
                .addClasspathResource("processe/leave.bpmn20.xml")
                // 图片文件
                .addClasspathResource("processe/leave.bpmn20.xml.png")
                .name("请假申请流程")
                .deploy();
        System.out.println("流程部署id:" + deployment.getId());
        System.out.println("流程部署名称:" + deployment.getName());
    return "OK";
    }


    @RequestMapping("/list")
    public Result<List<ProcessDefinition>> listRepository(){

        String processDefinitionKey = "my";
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        List<ProcessDefinition> list = processDefinitionQuery
//                .processDefinitionKey(processDefinitionKey)
                .orderByProcessDefinitionVersion().desc().list();

        log.info("=======list {}", ArrayUtil.toString(list));
        for (ProcessDefinition processDefinition : list) {
            System.out.println("------------------------");
            System.out.println(" 流 程 部 署 id ："+processDefinition.getDeploymentId());
            System.out.println("流程定义id：" + processDefinition.getId());
            System.out.println("流程定义名称" +processDefinition.getName());
            System.out.println("流程定义key：" + processDefinition.getKey());
            System.out.println("流程定义版本：" + processDefinition.getVersion());
        }

        return Result.buildSuccess(list);
    }
}
